package com.hwg.shiliupro.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hwg.shiliupro.dao.UserMapper;
import com.hwg.shiliupro.dao.WorkMapper;
import com.hwg.shiliupro.dto.PageBean;
import com.hwg.shiliupro.dto.WorkQueryDto;
import com.hwg.shiliupro.dto.WorkVo;
import com.hwg.shiliupro.po.Work;
import com.hwg.shiliupro.service.WorkService;
import com.hwg.shiliupro.utils.SimpleDateFormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class WorkServiceImpl implements WorkService {
    @Autowired
    private WorkMapper workMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 管理员得到所有的任务
     *
     * @return {@link List}<{@link Work}>
     */
    @Override
    public List<WorkVo> getAll() {
        List<Work> works = workMapper.selectList(null);
        List<WorkVo> workVos = new ArrayList<>();
        for (Work work : works) {
            WorkVo workVo = new WorkVo();
            workVo.setId(work.getId());
            workVo.setWorkName(work.getWorkName());
            workVo.setWorkContent(work.getWorkContent());
            workVo.setWorkStatus(work.getWorkStatus());
            String format = null;
            if(work.getFinishTime()!=null) {
                format = SimpleDateFormatUtil.getDateStrByDate(work.getFinishTime());
            }
            workVo.setFinishTime(format);
            workVo.setUId(work.getUId());
            workVo.setCheckStatus(work.getCheckStatus());
            workVo.setUserName(userMapper.selectById(work.getUId()).getName());
            workVos.add(workVo);
        }
        return workVos;
    }

    /**
     * 新增任务
     *
     * @param work 工作
     * @return {@link Boolean}
     */
    @Override
    public Boolean insert(Work work) {
        int count = workMapper.insertWork(work);
        return count > 0;
    }

    /**
     * 管理员审核  完成任务
     *
     * @return {@link Boolean}
     */
    @Override
    public Boolean ischeck(Integer id,Integer flag) {
        //创建一个work对象,用于mp的更新
        Work work = new Work();
        work.setId(id);
//        Date now = new Date();
//        work.setFinishTime(now);
        work.setCheckStatus(flag);
        int count = workMapper.updateById(work);
        return count > 0;
    }



    /**
     * 分页查询自己的任务
     *
     * @param workQueryDto 工作查询dto
     * @return {@link List}<{@link Work}>
     */
    @Override
    public PageBean<Work> selectByPage(WorkQueryDto workQueryDto) {
        //起始索引 = (当前页码-1)×每页显示记录数目
        int beginIndex = (workQueryDto.getPage() - 1) * workQueryDto.getSize();
        List<Work> works = workMapper.selectByPage(beginIndex,workQueryDto.getSize(),workQueryDto.getUId());
        QueryWrapper<Work> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("u_Id",workQueryDto.getUId());
        Integer count = workMapper.selectCount(queryWrapper);
        PageBean<Work> pageWork = new PageBean<>();
        pageWork.setRows(works);
        pageWork.setTotalCount(count);
        return pageWork;
    }

    /**
     * 用户确认完成/未完成任务
     *
     * @param id  id
     * @param map 地图
     * @return {@link Boolean}
     */
    @Override
    public Boolean isfinish(Integer id, Map map) {
        int flag = (int) map.get("flag");
        Work work = new Work();
        work.setId(id);
        work.setWorkStatus(flag);
        if(flag==1){
            Date now = new Date();
            work.setFinishTime(now);
        }else{
            work.setFinishTime(null);
            work.setCheckStatus(0);//未审核
        }
        int count = workMapper.updateFinish(work);
        return count > 0;
    }
}
